<?php
defined('_JEXEC') or die( 'Restricted access' );

class TablePlantplanOpe extends Fcore
{
	/** @var int Primary key */
	var $id					= 0;
	/** @var int */
	var $ppid				= 0;
	/** @var time */
	var $date				= '0000-00-00';
	/** @var int */
	var $type				= 0;
	/** @var int */
	var $oid				= 0;
	/** @var string */
	var $custom				= '';
	/** @var time */
	var $execution_time		= '0000-00-00';
	/** @var int */
	var $prid				= 0;
	

	/**
	* @param database A database connector object
	*/
	function __construct( &$db )
	{
		parent::__construct( '#__farm_plan', 'id', $db );
	}

	/**
	 * Binds an array to the object
	 * @param 	array	Named array
	 * @param 	string	Space separated list of fields not to bind
	 * @return	boolean
	 */
	function bind( $array, $ignore='' )
	{
		$result = parent::bind( $array );
		// cast properties
		$this->id	= (int) $this->id;
		return $result;
	}

	/**
	 * Overloaded check function
	 *
	 * @access public
	 * @return boolean
	 * @see JTable::check
	 * @since 1.5
	 */
	function check()
	{
//		if (!intval( $this->ppid ))
//		{
//			$this->setError(JText::_( '请选择农场' ));
//			return false;
//		}
//		
//		if (!intval( $this->pid ))
//		{
//			$this->setError(JText::_( '请选择植物' ));
//			return false;
//		}
//		
//		if (!intval( $this->uid ))
//		{
//			$this->setError(JText::_( '请选择记录人员' ));
//			return false;
//		}
//		
//		if (trim( $this->pre_sowing_date ) == '')
//		{
//			$this->setError(JText::_( '请输入计划播种日' ));
//			return false;
//		}
//		
//		if (trim( $this->pre_harvest ) == '')
//		{
//			$this->setError(JText::_( '请输入计划收割日' ));
//			return false;
//		}
//		
//		if (trim( $this->pre_production ) == '')
//		{
//			$this->setError(JText::_( '请输入预产量' ));
//			return false;
//		}
		return true;
	}
	
	/**
	 * 获得种植计划中未执行计划
	 * @param int $ppid 种植计划ID
	 * @param int $ppoid 具体某计划ID，弱水三千，只取一飘
	 * @return array
	 */
	public function getNotExecList($ppid, $ppoid=0)
	{		
		//种植详细计划
		$where = "WHERE ppid=" . intval($ppid) . " AND prid=0";
		if ($ppoid) $where .= " AND prid=" . intval($ppoid);
		$data = $this->getAll($where);
		if (empty($data)) return array();
		
		//加入施肥及农药信息
		$manureArr	= $pesticideArr = $return = array();
		$tManure	= JTable::getInstance('manure', 'Table');
		$tPesticide	= JTable::getInstance('pesticide', 'Table');
		foreach ((array)$data as $k => $v) {
			switch ($v['type']) {
				case 0://自定义
					break;
				case 1://施肥					
					empty($manureArr[$v['oid']]) && 
						$manureArr[$v['oid']] = $tManure->getOne($v['oid'], 'name');					
					$v['custom'] = $manureArr[$v['oid']];
					break;
				case 2://农药
					empty($pesticideArr[$v['oid']]) && 
						$pesticideArr[$v['oid']] = $tPesticide->getOne($v['oid'], 'name');
					$v['custom'] = $pesticideArr[$v['oid']];
					break;
			}
			$preTime = strtotime($v['date']);
			$return[$preTime][$v['id']] = array(
				'custom'	=> $v['custom'],
				'preDate'	=> $v['date'],
				'excDate'	=> $v['execution_time'],
				'prid'		=> $v['prid']
			);			
		}		
		ksort($return);
		
		return $return;
	}
}
